﻿// Gateway.Api 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
// 
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
// 
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using System.Text;
using Aurora.AI.Abstractions;
using Aurora.AI.Abstractions.ObjectModels.RequestModels.VideoRequestModel;
using Aurora.AI.Abstractions.ObjectModels.ResponseModels.VideoResponseModel;
using Aurora.AI.Abstractions.Video;
using Aurora.AI.MetaGLM.Models.RequestModels;

namespace Aurora.AI.MetaGLM.Video;

public class MetaGLMVideoService(Modules.Videos videos) : IAuroraVideoService
{
    public Task<VideoCreateTaskResponse> CreateVideoTask(VideoCreateRequest create,
        AuroraAIPlatformOptions options = null,
        CancellationToken cancellationToken = default)
    {
        StringBuilder sb = new();
        VideoRequestBase request = new();
        request.SetModel(create.Model);
        request.SetPrompt(create.Prompt);
        request.SetImageUrl(create.ImageUrl);
        var response = videos.GenerationTask(request, options.ApiKey);
        VideoCreateTaskResponse result = new();

        // 证明返回正常
        if (response?.error?.Count > 0)
        {
            response.error.TryGetValue("code", out var code);
            response.error.TryGetValue("message", out var message);
            result.Error = new Abstractions.Dtos.AuroraAIError();
            result.Error.Code = code;
            result.Error.Messages = new List<string> { message };
            return Task.FromResult(result);
        }

        if (response != null)
        {
            result.Id = response.id;
            result.TaskStatus = response.task_status;
            result.Model = response.model;
        }

        return Task.FromResult(result);
    }

    public Task<VideoCreateResponse> QueryVideoTask(VideoTaskQueryRequest query,
        AuroraAIPlatformOptions options = null,
        CancellationToken cancellationToken = default)
    {
        var response = videos.GenerationQuery(query.Id, options.ApiKey);
        VideoCreateResponse result = new();
        // 证明返回正常
        if (response?.error?.Count > 0)
        {
            response.error.TryGetValue("code", out var code);
            response.error.TryGetValue("message", out var message);
            result.Error = new Abstractions.Dtos.AuroraAIError
            {
                Code = code,
                Messages = [message]
            };
            return Task.FromResult(result);
        }

        if (response != null)
        {
            result.TaskStatus = response.task_status;
            result.Model = response.model;
            if (response.data?.Count > 0)
            {
                result.Results = response.data.Select(x => new VideoCreateResponse.VideoDataResult()
                {
                    CoverImageUrl = x.cover_image_url,
                    Url = x.url
                }).ToList();
            }
        }

        return Task.FromResult(result);
    }
}